home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Belgian Amiga Club - ADF Collection
/
BS1 part 41.zip
/
BS1 part 41
/
Compute`s Amiga resource 1.adf
/
Source
/
ALC
/
adviff.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-07
|
5KB
|
213 lines
/* These routines are extreme hacks that I haven't had time to revise;
you're at your own risk in using them! */
#include "adviff.h"
#include <stdio.h>
#include <exec/memory.h>
#include <graphics/gfx.h>
extern void *allocate_mem(); /* From ADVLASER.C */
read_iff_header(filename, header_struct)
char *filename;
struct iff_header *header_struct;
{
FILE *file;
if((file = fopen(filename, "r")) == NULL) /* Open for read-only */
return(RIFFH_NOOPEN);
if(fread(header_struct, 1, sizeof(struct iff_header), file) <
sizeof(struct iff_header))
{
fclose(file);
return(RIFFH_INCOMPLETE);
}
fclose(file);
if(strncmp(header_struct->form, "FORM", 4))
return(RIFFH_NOFORM);
if(strncmp(header_struct->ilbm, "ILBM", 4))
return(RIFFH_NOILBM);
return(RIFFH_SUCCESS);
}
load_iff(header, filename, colortable, bitmap)
struct iff_header *header;
char *filename;
WORD colortable[32];
struct BitMap *bitmap;
{
FILE *file;
UBYTE *image_buffer;
int i;
char in[4];
ULONG size;
if((file = fopen(filename, "r")) == NULL) /* Open for read-only */
return(LIFF_NOOPEN);
/* Set read position to the point right after the iff_header (color data) */
if(fseek(file, (long)sizeof(struct iff_header), 0) != 0)
{
fclose(file);
return(LIFF_NOSEEK);
}
/* If color table pointer specified, load it */
if(colortable != NULL)
for(i=0; i<header->cmap_size / 3; i++)
{
/* Read 3 bytes (red, green, blue) */
if(fread(in, 1, 3, file) < 3)
{
fclose(file);
return(LIFF_COLORERR);
}
colortable[i] =
((in[0] & 0xF0) << 4) | (in[1] & 0xF0) | ((in[2] & 0xF0) >> 4);
}
do /* Search for the body segment */
{
if(fread(in, 1, 4, file) < 4)
{
fclose(file);
return(LIFF_BODYERR);
}
} while(strncmp(in, "BODY", 4));
if(fread(&size, 4, 1, file) < 1)
{
fclose(file);
return(LIFF_SIZEERR);
}
if((image_buffer = allocate_mem(size, MEMF_PUBLIC)) == NULL)
{
fclose(file);
return(LIFF_MEMALLOCERR);
}
if(fread(image_buffer, 1, (UWORD)size, file) < (UWORD)size)
{
fclose(file);
return(LIFF_IMAGEREADERR);
}
if(get_data(image_buffer, header, bitmap) == FALSE)
{
fclose(file);
return(LIFF_GETDATAERR);
}
if(image_buffer != NULL) FreeMem(image_buffer, size);
fclose(file);
return(LIFF_SUCCESS);
}
int get_data(image_buffer, iffheader, bitmap)
UBYTE *image_buffer;
struct iff_header *iffheader;
struct BitMap *bitmap;
{
int status;
if(iffheader->compression == 0)
status = read_raw_iff(image_buffer, iffheader, bitmap);
else
status = iff_read(image_buffer, iffheader, bitmap);
return(status);
}
read_raw_iff(image_buffer, iffheader, bitmap)
UBYTE *image_buffer;
struct iff_header *iffheader;
struct BitMap *bitmap;
{
return(FALSE); /* NOT SUPPORTED! */
}
iff_read(image_buffer, iffheader, bitmap)
UBYTE *image_buffer;
struct iff_header *iffheader;
struct BitMap *bitmap;
{
int row, plane;
for(row=0; row<iffheader->height; row++)
for(plane=0; plane<iffheader->num_planes; plane++)
decompress(image_buffer, iffheader, bitmap, row, plane);
}
decompress(image_buffer, iffheader, bitmap, row, plane)
UBYTE *image_buffer;
struct iff_header *iffheader;
struct BitMap *bitmap;
unsigned int row, plane;
{
register int i;
register unsigned int offset, offinc;
static int dccount, dcptr, ffbytes, image_index;
static char decombuf[160], comp_flg, comp_byte;
dccount= (( (iffheader->width + 15) >> 4 ) << 1);
dcptr = 0;
while(dccount)
{
comp_flg = image_buffer[image_index++];
if(comp_flg >= 0)
{
comp_flg++;
dccount -= comp_flg;
for(i=0; i<comp_flg; i++)
decombuf[dcptr++] = image_buffer[image_index++];
}
else
if(comp_flg != -128)
{
comp_flg = -comp_flg+1;
dccount -= comp_flg;
comp_byte = image_buffer[image_index++];
for(i=0; i<comp_flg; ++i)
decombuf[dcptr++] = comp_byte;
}
}
tobitmap(bitmap, iffheader, row, plane, decombuf);
return(TRUE);
}
tobitmap(bitmap, iffheader, row, plane, decombuf)
struct BitMap *bitmap;
struct iff_header *iffheader;
UWORD row, plane;
char *decombuf;
{
UWORD ix, bytewidth, yoffset;
bytewidth = ((iffheader->width + 15) >> 4) << 1;
yoffset = row * bytewidth;
for(ix=0; ix<bytewidth; yoffset++)
{
*(bitmap->Planes[plane] + yoffset + (ix >> 1)) = decombuf[ix++];
*(bitmap->Planes[plane] + yoffset + (ix >> 1)) = decombuf[ix++];
}
}